package com.penghk.springbootdemo.rabbitMQ.spring;

/*
spring-rabbit主要API有：

MessageListenerContainer：监听容器，为消息入队提供异步处理
RabbitTemplate ：用来发送和接收消息
RabbitAdmin ：声明队列、交换器（Exchange）、绑定（Binding）

 */

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;


public class Main {


    public static void main(String[] args) throws InterruptedException {
        ConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);
        ((CachingConnectionFactory) connectionFactory).setUsername("guest");
        ((CachingConnectionFactory) connectionFactory).setPassword("guest");
        ((CachingConnectionFactory) connectionFactory).setVirtualHost("/");

        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);

        Queue queue = new Queue("springQueue");
        rabbitAdmin.declareQueue(queue);

        TopicExchange exchange = new TopicExchange("springTopicExchange");
        rabbitAdmin.declareExchange(exchange);

        rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo.*"));


        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        while (true) {
            template.convertAndSend("springTopicExchange", "foo.bar", "hello world bar");
//            template.convertAndSend("springTopicExchange", "foo.tot", "hello world tot");
            System.out.println("sending message");
            Thread.sleep(2000);
        }
    }




}
